﻿using Biz.Core;
using Biz.Core.Models;
using Microsoft.AspNetCore.Mvc;
using MiniFox.Components;
using MiniFox.Infrastructure;
using MiniFox.Platform;
using MiniFox.Resources;
using MiniFox.Utility;
using MiniFox.Web.Infrastructure;
using System.ComponentModel.DataAnnotations;

namespace Biz.WebApi.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [NoCache, PublicAccessible]
    public class DeviceController :MvcControllerBase
    {
        /// <summary>
        /// 自动装载服务组件
        /// </summary>
        [AutoWired]
        public IDeviceService DeviceService { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="deviceDto"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<DeviceVo> Create([FromBody] DeviceDto deviceDto)
        {
            var result = this.DeviceService.CreateDevice(deviceDto);
            return result.CreateResponse();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="deviceDto"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<DeviceVo> Update([FromBody] DeviceDto deviceDto)
        {
            var result = this.DeviceService.UpdateDevice(deviceDto);
            return result.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> Delete([FromBody] string deviceId)
        {
            var result = this.DeviceService.DeleteDevice(deviceId);
            return result.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<DeviceVo> Toggle([FromBody] string deviceId)
        {
            DeviceVo result = this.DeviceService.Toggle(deviceId);
            return result.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<DeviceVo> Get([FromQuery, Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string deviceId)
        {
            DeviceVo result = this.DeviceService.GetDevice(deviceId);
            return result.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="queryObject"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<ResultObject<IEnumerable<DeviceVo>>> Query([FromBody] PagedQueryObject<SearchModel> queryObject)
        {
            var result = this.DeviceService.QueryDeviceList(queryObject);
            return result.CreateResponse();
        }
    }
}
